package com.ming_za.odupdaterserver.core.thread;

import com.ming_za.odupdaterserver.OdUpdaterServerApplication;
import org.springframework.lang.NonNull;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class CustomizeNameThreadFactory implements ThreadFactory {

    protected final ThreadGroup group;
    protected final String threadName;
    protected final AtomicInteger threadNumber = new AtomicInteger(0);

    public CustomizeNameThreadFactory(String name) {
        group = Thread.currentThread().getThreadGroup();
        threadName = name;
    }

    @Override
    public Thread newThread(@NonNull Runnable r) {

        Thread t = new Thread(group,r,threadName + "-" + threadNumber.getAndIncrement());

        if (t.isDaemon())
            t.setDaemon(false);
        if (t.getPriority() != Thread.NORM_PRIORITY)
            t.setPriority(Thread.NORM_PRIORITY);

        return t;
    }

}
